// pages/mycollect/mycollect.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // resumeList: [],
    // resumeListMore: {page:1},

    jobList: [],
    jobListMore: {page:1},

    companyList: null,
    companyListMore: {page:1},

    // shopList: null,
    // shopListMore: {page:1},

    // courseList: null,
    // courseListMore: {page:1},
    nav:1

  },
  nav:function(e){
    this.setData({
      nav:e.currentTarget.dataset.index
    });
    if(this.data.nav == 1){
      this.data.jobList = [];
      this.fetchJob();
    }
    else if(this.data.nav==2 && this.data.companyList===null) {
      this.data.companyList = [];
      this.fetchCompany();
    }else if(this.data.nav==3 && this.data.shopList===null) {
      this.data.shopList = [];
      this.fetchShop();
    }else if(this.data.nav==4 && this.data.courseList===null) {
      this.data.courseList = [];
      this.fetchCourse();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userinfo = app.getUserinfo();
    this.setData({userinfo:userinfo});
    // this.fetchJob();
    // this.setData({nav:1});
    if(userinfo.group_id==1) {
      this.fetchJob();
      this.setData({nav:1});
    }else {
      this.fetchResume();
      this.setData({nav:0});
    }

    app.eventBus.on(app.Events.ResumeFavAdd, this.eventOnResumeFavAdd, this);
    app.eventBus.on(app.Events.ResumeFavRemove, this.eventOnResumeFavRemove, this);
    app.eventBus.on(app.Events.JobFavAdd, this.eventOnJobFavAdd, this);
    app.eventBus.on(app.Events.JobFavRemove, this.eventOnJobFavRemove, this);
    app.eventBus.on(app.Events.CompanyFavAdd, this.eventOnCompanyFavAdd, this);
    app.eventBus.on(app.Events.CompanyFavRemove, this.eventOnCompanyFavRemove, this);
    app.eventBus.on(app.Events.LabourFavAdd, this.eventOnLabourFavAdd, this);
    app.eventBus.on(app.Events.LabourFavRemove, this.eventOnLabourFavRemove, this);
    app.eventBus.on(app.Events.CourseFavAdd, this.eventOnCourseFavAdd, this);
    app.eventBus.on(app.Events.CourseFavRemove, this.eventOnCourseFavRemove, this);
  },
  onUnload() {
    app.eventBus.off(app.Events.ResumeFavAdd, this.eventOnResumeFavAdd);
    app.eventBus.off(app.Events.ResumeFavRemove, this.eventOnResumeFavRemove);
    app.eventBus.off(app.Events.JobFavAdd, this.eventOnJobFavAdd);
    app.eventBus.off(app.Events.JobFavRemove, this.eventOnJobFavRemove);
    app.eventBus.off(app.Events.CompanyFavAdd, this.eventOnCompanyFavAdd);
    app.eventBus.off(app.Events.CompanyFavRemove, this.eventOnCompanyFavRemove);
    app.eventBus.off(app.Events.LabourFavAdd, this.eventOnLabourFavAdd);
    app.eventBus.off(app.Events.LabourFavRemove, this.eventOnLabourFavRemove);
    app.eventBus.off(app.Events.CourseFavAdd, this.eventOnCourseFavAdd);
    app.eventBus.off(app.Events.CourseFavRemove, this.eventOnCourseFavRemove);
  },

  fetchResume() {
    util.fetch(this, 'xilujob.resume/fav_list', {}, 'resumeListMore', 'resumeList', 'data', 'unique');
  },
  fetchJob() {
    util.fetch(this, 'xilujob.job/fav_list', {}, 'jobListMore', 'jobList', 'data', 'unique');
  },
  fetchCompany() {
    util.fetch(this, 'xilujob.company/fav_list', {}, 'companyListMore', 'companyList', 'data', 'unique');
  },
  fetchShop() {
    util.fetch(this, 'labour/fav_list', {}, 'shopListMore', 'shopList', 'shops', 'unique');
  },
  fetchCourse() {
    util.fetch(this, 'course/fav_list', {}, 'courseListMore', 'courseList', 'courses', 'unique');
  },

  bindUnfavJob(event) {
    let index = event.currentTarget.dataset.index;
    let job = this.data.jobList[index];
    core.post({
      url: '/xilujob.job/toggle_fav', data: {job_id: job.id},
      success: ret => {
        if(!ret.data.is_fav) {
          app.eventBus.emit(app.Events.JobFavRemove, {job: job});
        }
      }
    });
  },
  bindApplyJob(event) {
    let index = event.currentTarget.dataset.index;
    let job = this.data.jobList[index];
    core.post({
      url: 'xilujob.job/apply', data: {job_id:job.id},
      success: ret => {
        wx.showToast({title: ret.msg,icon:'none'});
      }
    });
  },

  eventOnResumeFavAdd(data) {
    let resume = data.resume,
      index = util.getListItemIndexById(this.data.resumeList, resume.id);
    if(index===-1) {
      this.data.resumeList.splice(0,0,resume);
      this.setData({resumeList: this.data.resumeList});
    }
  },
  eventOnResumeFavRemove(data) {
    let resume = data.resume,
      index = util.getListItemIndexById(this.data.resumeList, resume.id);
    if(index!==-1) {
      this.data.resumeList.splice(index, 1);
      this.setData({resumeList: this.data.resumeList});
    }
  },

  eventOnJobFavAdd(data) {
    let job = data.job,
      index = util.getListItemIndexById(this.data.jobList, job.id);
    if(index===-1) {
      this.data.jobList.splice(0,0,job);
      this.setData({jobList: this.data.jobList});
    }
  },
  eventOnJobFavRemove(data) {
    let job = data.job,
      index = util.getListItemIndexById(this.data.jobList, job.id);
    if(index!==-1) {
      this.data.jobList.splice(index, 1);
      this.setData({jobList: this.data.jobList});
    }
  },

  eventOnCompanyFavAdd(data) {
    let company = data.company,
      index = util.getListItemIndexById(this.data.companyList, company.id);
    if(index===-1) {
      this.data.companyList.splice(0,0,company);
      this.setData({companyList: this.data.companyList});
    }
  },
  eventOnCompanyFavRemove(data) {
    let company = data.company,
      index = util.getListItemIndexById(this.data.companyList, company.id);
    if(index!==-1) {
      this.data.companyList.splice(index, 1);
      this.setData({companyList: this.data.companyList});
    }
  },

  eventOnLabourFavAdd(data) {
    let shop = data.shop,
      index = util.getListItemIndexById(this.data.shopList, shop.id);
    if(index===-1) {
      this.data.shopList.splice(0,0,shop);
      this.setData({shopList: this.data.shopList});
    }
  },
  eventOnLabourFavRemove(data) {
    let shop = data.shop,
      index = util.getListItemIndexById(this.data.shopList, shop.id);
    if(index!==-1) {
      this.data.shopList.splice(index, 1);
      this.setData({shopList: this.data.shopList});
    }
  },

  eventOnCourseFavAdd(data) {
    let course = data.course,
      index = util.getListItemIndexById(this.data.courseList, course.id);
    if(index===-1) {
      this.data.courseList.splice(0,0,course);
      this.setData({courseList: this.data.courseList});
    }
  },
  eventOnCourseFavRemove(data) {
    let course = data.course,
      index = util.getListItemIndexById(this.data.courseList, course.id);
    if(index!==-1) {
      this.data.courseList.splice(index, 1);
      this.setData({courseList: this.data.courseList});
    }
  },

  unfav: function (event) {
    let index = event.currentTarget.dataset.index;
    // this.data.talentsList.splice(index, 1);
    // this.setData({talentsList: this.data.talentsList});
    let resume = this.data.resumeList[index];
    wx.showModal({
      title: '提示',
      content: '是否取消收藏？',
      success: res => {
        if(res.confirm){
          core.post({
            url: '/xilujob.resume/toggle_fav', data: {id: resume.id},
            success: ret => {
              if(!ret.data.is_fav) {
                app.eventBus.emit(app.Events.ResumeFavRemove, {resume: resume});
              }
            }
          });
        }
      }
    })
    
  },
  createInterview: function (event) {
    wx.navigateTo({url: '/pages/interview_take/interview_take'})
  }

});
